<!--
  * 全景图添加
  *
  * @Author:    骆伟林
  * @Date:      2025-10-22 17:09:49
  * @Copyright  软件需求工程第八组
-->
<template>
  <a-modal :title="form.pitureId ? '编辑' : '添加'" :width="800" :open="visibleFlag" @cancel="onClose" :maskClosable="false" :destroyOnClose="true">
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }" class="smart-query-form">
      <a-row class="smart-query-form-row">
        <a-col :span="12">
          <a-form-item label="地点" name="addressId">
            <a-select v-model:value="form.addressId" placeholder="所属地点" style="flex: 1; margin-right: 8px" @change="changeAddress">
              <a-select-option v-for="address in addressList" :key="address.addressId" :value="address.addressId">
                <div style="display: flex; justify-content: space-between; align-items: center; width: 100%">
                  <div style="flex: 1; overflow: hidden">
                    <div style="margin-right: 5px; text-overflow: ellipsis; overflow: hidden; white-space: nowrap">
                      {{ address.addressName }}
                    </div>
                  </div>
                </div>
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="图片名称" name="pictureName">
            <a-input style="width: 100%" v-model:value="form.pictureName" placeholder="图片名称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="拍摄日期" name="pictureTime">
            <a-date-picker show-time valueFormat="YYYY-MM-DD HH:mm:ss" v-model:value="form.pictureTime" style="width: 100%" placeholder="拍摄日期" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="图片地址" name="picture">
            <Upload
              accept=".jpg,.jpeg,.png,.gif"
              :maxUploadSize="1"
              buttonText="点击上传图片"
              :default-file-list="form.pictureUrl || []"
              listType="picture-card"
              @change="(fileList) => changeAttachment(fileList)"
              :folder="FILE_FOLDER_TYPE_ENUM.FEEDBACK.value"
              :disabled="isDetail"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>
<script setup>
  import { reactive, ref, nextTick, onMounted } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { pictureApi } from '/@/api/business/picture/picture-api';
  import { smartSentry } from '/@/lib/smart-sentry';
  import Upload from '/@/components/support/file-upload/index.vue';
  import { FILE_FOLDER_TYPE_ENUM } from '/@/constants/support/file-const';

  function changeAttachment(fileList) {
    form.pictureUrl = fileList;
  }

  const addressList = ref([]);
  async function getAddressList() {
    try {
      const res = await pictureApi.getAddressList();
      addressList.value = res.data;
    } catch (err) {
      console.error('获取地点列表失败:', err);
    }
  }

  function changeAddress(value) {
    console.log(value);
    addressList.value.find((item) => {
      if (item.addressId === value) {
        form.addressName = item.addressName;
      }
    });
  }
  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, {
        ...rowData,
        pictureUrl: Array.isArray(rowData.pictureUrl) ? rowData.pictureUrl : [],
      });
    }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
    pitureId: undefined, //id
    addressId: undefined, //地点id
    addressName: undefined,
    pictureName: undefined, //图片名称
    pictureUrl: undefined, //图片地址
    pictureTime: undefined, //全景图拍摄日期
  };

  let form = reactive({ ...formDefault });

  const rules = {
    addressId: [{ required: true, message: '地点id 必填' }],
    pictureName: [{ required: true, message: '图片名称 必填' }],
    pictureUrl: [{ required: true, message: '图片地址必填' }],
    pictureTime: [{ required: true, message: '全景图拍摄日期 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.pitureId) {
        await pictureApi.update(form);
      } else {
        await pictureApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });

  onMounted(() => {
    getAddressList();
  });
</script>
